package org.koon;

import org.koon.jadelyre.KoonPacket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoonResponse extends KoonPacket {
    private HttpStatus httpStatus = HttpStatus.OK;
    private KooRequest httpRequest;
    private Map<byte[], byte[]> headers = new HashMap<>();
    private List<byte[]> cookieList = new ArrayList<>();
    private int headerLength;
    private int cookieLength;

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public KooRequest getHttpRequest() {
        return httpRequest;
    }

    public void setHttpRequest(KooRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    public int getHeaderLength() {
        return headerLength;
    }

    public int getCookieLength() {
        return cookieLength;
    }

    public Map<byte[], byte[]> getHeaders() {
        return headers;
    }

    public List<byte[]> getCookieList() {
        return cookieList;
    }

    public MoonResponse putHeader(String name, String value){
        byte[] nameBytes = name.getBytes();
        byte[] valueBytes = value.getBytes();
        headerLength += nameBytes.length + valueBytes.length + 4;//4是考虑冒号,value前空格符,回车符,换行符
        headers.put(nameBytes,valueBytes);
        return this;
    }
    public MoonResponse putCookie(Cookie cookie){
        byte[] bytes = cookie.toString().getBytes();
        cookieList.add(bytes);
        cookieLength += bytes.length + Header.SET_COOKIE.getValue().getBytes().length + 4;//冒号，空格，回车，换行 todo 这里后期优化成固定数字
        return this;
    }
}
